<template>
  <div class="identity-verification-container">
    <div class="verification-card">
      <div class="card-header">
        <el-icon class="header-icon"><UserFilled /></el-icon>
        <h2>实名认证</h2>
        <p>为了保障您的账户安全，请完成实名认证</p>
      </div>

      <div class="info-box">
        <h3><el-icon><InfoFilled /></el-icon> 认证须知</h3>
        <ul>
          <li>请确保填写的姓名与身份证上的姓名完全一致</li>
          <li>身份证号码将用于验证您的身份信息</li>
          <li>认证信息将被加密保存，仅用于身份验证</li>
          <li>完成认证后才能使用房东、中介等高级功能</li>
        </ul>
      </div>

      <el-form
        ref="verificationForm"
        :model="form"
        :rules="rules"
        label-width="120px"
        class="verification-form"
      >
        <el-form-item label="真实姓名" prop="realName">
          <el-input
            v-model="form.realName"
            placeholder="请输入您的真实姓名"
            :prefix-icon="User"
            clearable
          />
        </el-form-item>

        <el-form-item label="身份证号码" prop="identityCard">
          <el-input
            v-model="form.identityCard"
            placeholder="请输入18位身份证号码"
            :prefix-icon="CreditCard"
            clearable
            maxlength="18"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="submitVerification"
            class="submit-btn"
          >
            <el-icon v-if="!loading"><Check /></el-icon>
            {{ loading ? '认证中...' : '提交认证' }}
          </el-button>
          <el-button @click="goBack" class="back-btn">
            <el-icon><ArrowLeft /></el-icon>
            返回
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  UserFilled,
  InfoFilled,
  User,
  CreditCard,
  Check,
  ArrowLeft
} from '@element-plus/icons-vue'
import api from '@/utils/api'
import { getToken } from '@/utils/auth'

const router = useRouter()
const verificationForm = ref()
const loading = ref(false)

// 表单数据
const form = reactive({
  realName: '',
  identityCard: ''
})

// 验证规则
const rules = {
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' },
    { 
      pattern: /^[\u4e00-\u9fa5]+$/, 
      message: '真实姓名必须为中文', 
      trigger: 'blur' 
    },
    { 
      min: 2, 
      max: 10, 
      message: '姓名长度应在2-10个字符之间', 
      trigger: 'blur' 
    }
  ],
  identityCard: [
    { required: true, message: '请输入身份证号码', trigger: 'blur' },
    { 
      pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, 
      message: '身份证号码格式不正确', 
      trigger: 'blur' 
    }
  ]
}

// 提交实名认证
const submitVerification = async () => {
  try {
    // 表单验证
    await verificationForm.value.validate()
    
    // 确认提交
    await ElMessageBox.confirm(
      '请确认您填写的信息准确无误，提交后将无法修改',
      '确认提交',
      {
        confirmButtonText: '确认提交',
        cancelButtonText: '再次检查',
        type: 'warning'
      }
    )

    loading.value = true

    // 调用API
    const response = await api.post('/user/verify-identity', {
      realName: form.realName,
      identityCard: form.identityCard
    }, {
      headers: {
        'info': getToken()
      }
    })

    if (response.data.code === 200) {
      ElMessage.success('实名认证提交成功！')
      
      // 延迟跳转到个人资料页面
      setTimeout(() => {
        router.push('/user/profile')
      }, 1500)
    } else {
      ElMessage.error(response.data.message || '认证失败，请重试')
    }
  } catch (error) {
    if (error !== 'cancel') {
      console.error('实名认证失败:', error)
      ElMessage.error('网络错误，请检查网络连接后重试')
    }
  } finally {
    loading.value = false
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}
</script>

<style scoped>
.identity-verification-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.verification-card {
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 600px;
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.card-header {
  text-align: center;
  margin-bottom: 30px;
}

.header-icon {
  font-size: 48px;
  color: #667eea;
  margin-bottom: 15px;
}

.card-header h2 {
  color: #333;
  font-size: 28px;
  margin-bottom: 10px;
  font-weight: 600;
}

.card-header p {
  color: #666;
  font-size: 16px;
}

.info-box {
  background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%);
  border: 1px solid #e1f5fe;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 30px;
}

.info-box h3 {
  color: #1976d2;
  margin-bottom: 15px;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.info-box ul {
  color: #333;
  font-size: 14px;
  line-height: 1.6;
  margin-left: 20px;
}

.info-box li {
  margin-bottom: 8px;
}

.verification-form {
  margin-top: 20px;
}

.verification-form :deep(.el-form-item__label) {
  font-weight: 500;
  color: #333;
}

.verification-form :deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.verification-form :deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.verification-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.submit-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 8px;
  padding: 12px 30px;
  font-size: 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  margin-right: 15px;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.back-btn {
  border-radius: 8px;
  padding: 12px 20px;
  font-size: 16px;
  transition: all 0.3s ease;
}

.back-btn:hover {
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .verification-card {
    padding: 30px 20px;
    margin: 10px;
  }
  
  .card-header h2 {
    font-size: 24px;
  }
  
  .verification-form :deep(.el-form-item__label) {
    font-size: 14px;
  }
}
</style>
